package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.google.gson.Gson;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.MallAdapter;
import com.maila.xiaoaoschool.api.FindApi;
import com.maila.xiaoaoschool.entity.XAmallClass;
import com.maila.xiaoaoschool.entity.XAmallGoods;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.TabUtils;
import com.maila.xiaoaoschool.widget.MyRadioHorizontalScrollView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/30.
 */

public class XiaoAoMallActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_namee)
    TextView tvShopNamee;
    @BindView(R.id.ll_title)
    AutoLinearLayout llTitle;
    @BindView(R.id.rhv_mall)
    TabLayout rhvMall;
    @BindView(R.id.lv_fujin)
    GridView lvFujin;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    Gson gson;
    List<String> classids = new ArrayList<>();
    MallAdapter mallAdapter;
    private String type = "0";
    private int page = 1;
    private int size = 8;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall);
        ButterKnife.bind(this);
        gson = new Gson();
        mallAdapter = new MallAdapter(this);
        lvFujin.setAdapter(mallAdapter);
        initdata();
    }

    private void initdata() {
        initclass();
        initGoods();
    }

    /**
     * 获取商品列表
     */
    private void initGoods() {
        new FindApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                XAmallGoods xAmallGoods = gson.fromJson(result, XAmallGoods.class);
                if (xAmallGoods.getResult() == 1) {
                    if (page == 1) {
                        mallAdapter.setMdata(xAmallGoods.getData());
                    } else {
                        mallAdapter.addMdata(xAmallGoods.getData());
                    }
                    hotRefresh.finishRefreshLoadMore();
                    ++page;
                } else {
                    hotRefresh.finishRefreshLoadMore();
                    hotRefresh.setLoadMore(false);
                }

            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getXiaoMallgoods(type, page, size);
    }

    /**
     * 初始化分类
     */
    private void initclass() {
        new FindApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                XAmallClass xAmallClass = gson.fromJson(result, XAmallClass.class);
                if (xAmallClass.getResult() == 1) {
                    initab(xAmallClass.getData());

                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getXiaoMallclass();
    }

    private void initab(List<XAmallClass.DataBean> data) {
        rhvMall.addTab(rhvMall.newTab().setText("全部"));
        classids.add(type);
        for (int i = 0; i < data.size(); i++) {
            rhvMall.addTab(rhvMall.newTab().setText(data.get(i).getType_name()));
            classids.add(data.get(i).getType_id());
        }
        rhvMall.post(new Runnable() {
            @Override
            public void run() {
                try {
                    TabUtils.settab(XiaoAoMallActivity.this,rhvMall);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        rhvMall.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                type = classids.get(tab.getPosition());
                page=1;
                initGoods();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick(R.id.white_return_iv)
    public void onViewClicked() {
        finish();
    }
}
